package ar.uba.fi.tonyvaliente.text.filtering;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;

import ar.uba.fi.tonyvaliente.utils.RecursosAplicacion;

public class StemmingFilter implements TextFilter {
	
	private static final String SETTINGS_ROOT = RecursosAplicacion.instance().getProperty("settings.root");

	@Override
	public LinkedList<String> filter(LinkedList<String> text) {
		LinkedList<String> result = new LinkedList<String>();

		if (verbs == null)
			populateVerbs();

		for (String word : text) {
			StemResult stemResult = performStem(word);
			if (stemResult.isVerb()) {
				result.add(stemResult.getVerb());
			} else {
				result.add(word);
			}
		}

		return result;
	}

	private StemResult performStem(String word) {
		if (verbs.contains(word))
			return new StemResult(true, word);

		for (String suffix : verbsBySuffix.keySet()) {
			for (String stemmedVerb : verbsBySuffix.get(suffix)) {
				if (!word.startsWith(stemmedVerb))
					continue;
				
				for (String stem : stemRules) {
					if(word.endsWith(stem))
						continue;
					
					if (word.equals(stemmedVerb + stem)) {
						return new StemResult(true, stemmedVerb + suffix);
					}
				}
			}
		}

		return new StemResult(false);
	}

	private class StemResult {

		private boolean isVerb;
		private String verb;

		public StemResult(boolean isVerb) {
			this(isVerb, null);
		}

		public StemResult(boolean isVerb, String verb) {
			super();
			this.isVerb = isVerb;
			this.verb = verb;
		}

		public boolean isVerb() {
			return this.isVerb;
		}

		public String getVerb() {
			return this.verb;
		}
	}

	private static LinkedList<String> verbs = null;
	private static HashMap<String, LinkedList<String>> verbsBySuffix = new HashMap<String, LinkedList<String>>();
	private static LinkedList<String> stemRules = new LinkedList<String>();

	private static void populateVerbs() {
		verbs = new LinkedList<String>();

		File file = new File(SETTINGS_ROOT + "/verbs.txt");
		BufferedReader reader;
		try {
			reader = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}

		try {
			String line;
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				if (line.equals(""))
					continue;
				if (line.startsWith("#"))
					continue;

				verbs.add(line);

				String stem = line.substring(0, line.length() - 2);
				String suffixKey = line.substring(line.length() - 2, line
						.length());
				if (!verbsBySuffix.containsKey(suffixKey))
					verbsBySuffix.put(suffixKey, new LinkedList<String>());
				verbsBySuffix.get(suffixKey).add(stem);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		file = new File(SETTINGS_ROOT + "/stem_rules.txt");
		try {
			reader = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		}

		try {
			String line;
			while ((line = reader.readLine()) != null) {
				line = line.trim();
				if (line.equals(""))
					continue;
				if (line.startsWith("#"))
					continue;

				String stem = line.substring(1);
				stemRules.add(stem);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
